package dao.impl;

import bean.ScreeningSchedule;
import com.mysql.cj.jdbc.ConnectionImpl;
import dao.ScreeningScheduleDao;
import dao.VideoHallDao;
import util.DBCon;
import util.DataTypeConversion;

import java.lang.invoke.VolatileCallSite;
import java.net.ServerSocket;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ScreeningScheduleDaoImpl implements ScreeningScheduleDao {
    @Override
    public List<ScreeningSchedule> getScreeningSchedulesByMovieId(int movieId) {
        Connection con = DBCon.getConnection();
        if (con == null) {
            System.out.println("数据库连接失败");
        } else {
            ResultSet rs = null;
            List<ScreeningSchedule> screeningScheduleList = new ArrayList<>();
            try {
                PreparedStatement preparedStatement = con.prepareStatement("Select * from table_screening_schedule where m_id = ?");
                preparedStatement.setInt(1, movieId);
                rs = preparedStatement.executeQuery();
                while (rs.next()) {
                    ScreeningSchedule screeningSchedule = new ScreeningSchedule();
                    screeningSchedule.setId(rs.getInt("screening_schedule_id"));
                    screeningSchedule.setMovie(new MovieDaoImpl().getMovieById(rs.getInt("m_id")));
                    screeningSchedule.setVideoHall(new VideoHallDaoImpl().getVideoById(rs.getInt("video_hall_id")));
                    screeningSchedule.setSeatNum(rs.getInt("seat_num"));
                    screeningSchedule.setStartTime(rs.getTimestamp("start_time"));
                    screeningSchedule.setEndTime(rs.getTimestamp("end_time"));
                    screeningScheduleList.add(screeningSchedule);
                }
                return screeningScheduleList;
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        return null;
    }

    @Override
    public boolean addScreeningSchedule(ScreeningSchedule screeningSchedule) {
        Connection con = DBCon.getConnection();
        if (con == null) {
            System.out.println("数据库链接失败");
            return false;
        }
        try {
            PreparedStatement preparedStatement = con.prepareStatement("insert into  table_screening_schedule(video_hall_id,m_id,price,start_time,end_time,seat_num) values(?,?,?,?,?,?) ");
            preparedStatement.setInt(1, screeningSchedule.getVideoHall().getId());
            preparedStatement.setInt(2, screeningSchedule.getMovie().getId());
            preparedStatement.setBigDecimal(3, screeningSchedule.getMovie().getPrice());
            preparedStatement.setTimestamp(4, Timestamp.valueOf((DataTypeConversion.conversionUtilDateToString(screeningSchedule.getStartTime()))));
            preparedStatement.setTimestamp(5, Timestamp.valueOf(DataTypeConversion.conversionUtilDateToString(screeningSchedule.getEndTime())));
            preparedStatement.setInt(6, screeningSchedule.getVideoHall().getSeatNum());
            int addNum = preparedStatement.executeUpdate();
            if (addNum > 0) {
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    @Override
    public ScreeningSchedule getScreeningScheduleById(int id) {
        Connection con = DBCon.getConnection();
        if (con == null) {
            System.out.println("数据库链接失败");
            return null;
        }
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = con.prepareStatement("Select * from table_screening_schedule where screening_schedule_id = ?");
            preparedStatement.setInt(1, id);
            ResultSet rs = preparedStatement.executeQuery();
            if (rs.next()) {
                ScreeningSchedule screeningSchedule = new ScreeningSchedule();
                screeningSchedule.setId(id);
                screeningSchedule.setMovie(new MovieDaoImpl().getMovieById(rs.getInt("m_id")));
                screeningSchedule.setVideoHall(new VideoHallDaoImpl().getVideoById(rs.getInt("video_hall_id")));
                screeningSchedule.setSeatNum(rs.getInt("seat_num"));
                screeningSchedule.setStartTime(rs.getTimestamp("start_time"));
                screeningSchedule.setEndTime(rs.getTimestamp("end_time"));
                return screeningSchedule;
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;

    }
    public boolean deleteScreen(int screenId) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = DBCon.getConnection();
            preparedStatement = connection.prepareStatement("DELETE FROM table_screening_schedule WHERE screening_schedule_id = ?");
            preparedStatement.setInt(1,screenId);
            int deleteNum = preparedStatement.executeUpdate();
            if(deleteNum>0){
                System.out.println("删除成功");
                return true;
            }
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    public List<ScreeningSchedule> getallScreeningSchedules() {
        List<ScreeningSchedule> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = DBCon.getConnection();
            preparedStatement = connection.prepareStatement("select * from table_screening_schedule");

            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                ScreeningSchedule screeningSchedule = new ScreeningSchedule();
//                Cinema cinema = new Cinema();
//                VideoHall videoHall = new VideoHall();
//                cinema.setName(resultSet.getString("cinema_name"));
//                videoHall.setId(resultSet.getInt("video_hall_id"));
//                videoHall.setName(resultSet.getString("video_hall_name"));

//                screeningSchedule.setCinema(cinema);
//                screeningSchedule.setVideoHall(videoHall);
                screeningSchedule.setId(resultSet.getInt("screening_schedule_id"));
                screeningSchedule.setStartTime(resultSet.getTimestamp("start_time"));
                screeningSchedule.setEndTime(resultSet.getTimestamp("end_time"));
                screeningSchedule.setSeatNum(resultSet.getInt("seat_num"));
                screeningSchedule.setMovie(new MovieDaoImpl().getMovieById(resultSet.getInt("m_id")));
                screeningSchedule.setVideoHall(new VideoHallDaoImpl().getVideoById(resultSet.getInt("video_hall_id")));
                screeningSchedule.setCinema(new CinemaDaoImpl().getCinemaById(new VideoHallDaoImpl().getVideoById(resultSet.getInt("video_hall_id")).getCinemaId()));
                list.add(screeningSchedule);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("Error fetching screening schedules", e);
        }

        return list;
    }

}
